package com.program.beautymakeup.utils;


import com.program.beautymakeup.domain.vo.ResultInfo;

import static com.program.beautymakeup.utils.RequestUtil.getCurrentUrl;

/**
 * 公共返回对象工具类
 *
 * @author CaiKe
 */
public class ResultInfoUtil {

    /**
     * 请求出错返回
     */
    public static <T> ResultInfo<T> buildError() {
        return build(404, "ERROR",  null);
    }


    public static <T> ResultInfo<T> buildError(String message) {
        return build(404, message, null);
    }


    public static <T> ResultInfo<T> buildError(int code, String message) {
        return build(404, message, null);
    }

    public static <T> ResultInfo<T> buildSuccess() {
        return build(200, "SUCCESS", null);
    }

    /**
     * 请求成功返回
     */
    public static <T> ResultInfo<T> buildSuccess(T data) {
        return build(200,"", data);
    }

    public static <T> ResultInfo<T> buildSuccess(String message) {
        return build(200, message, null);
    }
    public static <T> ResultInfo<T> buildSuccess(String message,T date) {
        return build(200, message, date);
    }
    /**
     * 如果需要返回一个字符串，则使用该方法，否则该字符串将会作为message而不是data
     *
     * @param data
     * @return
     */
    public static ResultInfo<String> buildSuccessWithStr(String data) {
        return build(200, null, data);
    }


    /**
     * 构建返回对象方法
     */
    public static <T> ResultInfo<T> build(Integer code, String message, T data) {
        if (code == null) {
            code = 200;
        }
        if (message == null) {
            message = "SUCCESS";
        }
        return ResultInfo.<T>builder()
                .code(code)
                .message(message)
                .path(getCurrentUrl())
                .data(data)
                .build();
    }
}